package indi.jp.project.business.pub.service.impl;

import indi.jp.project.business.pub.dao.OfficeDAO;
import indi.jp.project.business.pub.pojo.po.OfficePO;
import indi.jp.project.business.pub.pojo.query.OfficeQuery;
import indi.jp.project.business.pub.service.OfficeService;
import indi.jp.project.core.entity.Datagrid;
import indi.jp.project.core.entity.JsonResult;
import indi.jp.project.core.util.SerialUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 办事处
 *
 * @author jiangping
 * @date 2022/6/5
 */
@Service
public class OfficeServiceImpl implements OfficeService {

    @Resource
    private OfficeDAO officeDAO;

    @Resource
    private SerialUtil serialUtil;

    @Override
    public JsonResult queryGrid(OfficeQuery query) throws Exception {
        List<OfficePO> list = officeDAO.queryList(query);
        int count = officeDAO.queryCount(query);
        Datagrid<OfficePO> datagrid = new Datagrid<>(list, count);
        return JsonResult.success(datagrid);
    }

    @Override
    public JsonResult save(OfficePO officePO) throws Exception {
        if(officePO.getId() == null){
            String id = String.valueOf(serialUtil.nextId());
            officePO.setId(id);
            officeDAO.insert(officePO);
        } else {
            officeDAO.update(officePO);
        }
        return JsonResult.success();
    }

    @Override
    public JsonResult findById(String id) throws Exception {
        OfficePO officePO = officeDAO.findById(id);
        return JsonResult.success(officePO);
    }

    @Override
    public JsonResult deleteById(String id) throws Exception {
        officeDAO.deleteById(id);
        return JsonResult.success();
    }
}
